//: Filename: test.cpp
//: test.cpp


#include "test.h"

using namespace std;

Man::Man() 
{
    scores = new double[2];
    scores[0] = 0;
    scores[1] = 10;
}

void Man::setAge(int age)
{
    _age = age;
    //cout << _age << endl;
}

void Man::setName()
{
    _name = "Man";
    //cout << _name << endl;
}

Man::~Man()
{
    cout << _name << "'s scores[0] is "<< scores[0] << endl;
    delete[] scores;
}

void Boy::setAge() 
{
    _age = 15;
    cout << _age << endl;
}

void Boy::setName()
{
    _name = "Boy";
    cout << _name << endl;
}

void testDynamicArray1()
{
    int m = 1000000;
    int n = 1000000;
    deqb2 deqb2Arr(m, deqb(n));
    vecb2 vecb2Arr(m, vecb(n));
    vector<double> vecbArr(m*n);
    double** b2Arr;
    b2Arr = new double*[m];
    for(int i=0; i < m; i++) b2Arr[i] = new double[n];
    int maxIteration = 10000;

    int index = 0;
    clock_t start_t = clock();
    while(index <= maxIteration)
    {
        for(int i=0; i<m; i++)
        {
            for(int j=0; j<n;j++)
            {
                deqb2Arr[i][j] = i + j;
            }
        }
        index++;
    }
    clock_t final_t = clock();
    cout << "deqb2Arr: " << (final_t - start_t) / CLOCKS_PER_SEC << "sec" << endl;

    index = 0;
    start_t = clock();
    while(index <= maxIteration)
    {
        for(int i=0; i<m; i++)
        {
            for(int j=0; j<n;j++)
            {
                vecb2Arr[i][j] = i + j;
            }
        }
        index++;
    }
    final_t = clock();
    cout << "vecb2Arr: " << (final_t - start_t) / CLOCKS_PER_SEC << "sec" << endl;

    index = 0;
    start_t = clock();
    while(index <= maxIteration)
    {
        for(int i=0; i<m; i++)
        {
            for(int j=0; j<n;j++)
            {
                vecbArr[i*n + j] = i + j;
            }
        }
        index++;
    }
    final_t = clock();
    cout << "vecbArr: " << (final_t - start_t) / CLOCKS_PER_SEC << "sec" << endl;

    index = 0;
    start_t = clock();
    while(index <= maxIteration)
    {
        for(int i=0; i<m; i++)
        {
            for(int j=0; j<n;j++)
            {
                b2Arr[i][j] = i + j;
            }
        }
        index++;
    }
    final_t = clock();
    cout << "b2Arr: " << (final_t - start_t) / CLOCKS_PER_SEC << "sec" << endl;
} //: testDynamicArray1

void testDynamicArray2()
{
    int m = 1000;
    int n = 1000;
    vecb2 vecb2Arr(m, vecb(n));
    vector<double> vecbArr(m*n);
    double** b2Arr;
    b2Arr = new double*[m];
    for(int i=0; i < m; i++) b2Arr[i] = new double[n];
    double* bArr = new double[m*n];
    int maxIteration = 10000;

    int index;
    clock_t start_t, final_t;

    index = 0;
    start_t = clock();
    while(index <= maxIteration)
    {
        for(int i=0; i<m; i++)
        {
            for(int j=0; j<n;j++)
            {
                vecb2Arr[i][j] = 1.0;
            }
        }
        index++;
    }
    final_t = clock();
    cout << "vecb2Arr: " << (final_t - start_t) / CLOCKS_PER_SEC << "sec" << endl;

    index = 0;
    start_t = clock();
    while(index <= maxIteration)
    {
        for(vecb2::iterator i=vecb2Arr.begin(); i!=vecb2Arr.end(); i++)
        {
            for(int j=0; j < n;j++)
            {
                (*i)[j] = 1.0;
            }
        }
        index++;
    }
    final_t = clock();
    cout << "vecb2ArrIter: " 
         << (final_t - start_t) / CLOCKS_PER_SEC << "sec" << endl;

    index = 0;
    start_t = clock();
    while(index <= maxIteration)
    {
        for(vecb2::iterator i=vecb2Arr.begin(); i!=vecb2Arr.end(); i++)
        {
            for(vecb::iterator j=(*i).begin(); j != (*i).end();j++)
            {
                (*j) = 1.0;
            }
        }
        index++;
    }
    final_t = clock();
    cout << "vecb2ArrIterIter: " 
         << (final_t - start_t) / CLOCKS_PER_SEC << "sec" << endl;

    index = 0;
    start_t = clock();
    while(index <= maxIteration)
    {
        for(int i=0; i<m; i++)
        {
            for(int j=0; j<n;j++)
            {
                vecbArr[i*n + j] = 1.0;
            }
        }
        index++;
    }
    final_t = clock();
    cout << "vecbArr: " << (final_t - start_t) / CLOCKS_PER_SEC << "sec" << endl;

    index = 0;
    start_t = clock();
    while(index <= maxIteration)
    {
        vecb::iterator iter = vecbArr.begin();
        for(int i=0; i<m; i++)
        {
            for(int j=0; j<n;j++)
            {
                *(iter+i*n+j) = 1.0;
            }
        }
        index++;
    }
    final_t = clock();
    cout << "vecbArrIter: " << (final_t - start_t) / CLOCKS_PER_SEC << "sec" << endl;

    index = 0;
    start_t = clock();
    while(index <= maxIteration)
    {
        for(int i=0; i<m; i++)
        {
            for(int j=0; j<n;j++)
            {
                b2Arr[i][j] = 1.0;
            }
        }
        index++;
    }
    final_t = clock();
    cout << "b2Arr: " << (final_t - start_t) / CLOCKS_PER_SEC << "sec" << endl;

    index = 0;
    start_t = clock();
    while(index <= maxIteration)
    {
        for(int i=0; i<m; i++)
        {
            for(int j=0; j<n;j++)
            {
                bArr[i*n + j] = 1.0;
            }
        }
        index++;
    }
    final_t = clock();
    cout << "bArr: " << (final_t - start_t) / CLOCKS_PER_SEC << "sec" << endl;

} //: testDynamicArray2

/*
 *testListPtr
 */
void testListPtr()
{
    vector<int*> aVec(10, new int);
    /*
    for(iter = aList.begin(); iter != aList.end(); iter++)
    {
        (*(*iter))
    }
    */
    int* aint;
    aint = new int;
    *aint = 10;
    cout << "1" << endl;
    for(int i=0; i<10; i++)
    {
        //aVec[i] = aint;
        cout << (aVec[i]) << endl;
    }

} //: testListPtr

/*
 *testStruct1
 */
void testStruct1()
{
    students *liji = new students;
    liji->age = 31;
    liji->scores = new double[2];
    liji->scores[0] = 88;
    liji->scores[1] = 90;
    cout << liji->age << endl;
    cout << liji->scores[0] << endl;
    cout << liji->scores[1] << endl;

    delete[] liji->scores;
    delete[] liji;
    double* x = new double[10];
    double* b = x;
    x = new double[10];
    delete[] b;
    delete[] x;
} //: testStruct1

struct aa
{
    int age;
    double books;
};

/*
 *testContainer
 */
void testContainer()
{
    vector<double> a(10, 1.0);
    vector<double>::iterator iter;
    iter = a.begin();
    for(int i=0; i<10; i++, iter++)
    {
        a[i] = i;
        cout << a[i] << "-----" << *iter << endl;
        cout << &(a[i]) << "-----" << &(*iter) << endl;

    }
    iter--;
    cout << *iter << endl;

    vector<aa> vStruct;
    aa bb;
    vStruct.push_back(bb);
    bb.age = 10;
    vStruct[0].age = 100;
    cout << bb.age << "-----" << vStruct[0].age << endl;
    vector<aa>::iterator itera = vStruct.begin();
    cout << &bb << "-----" <<&(vStruct[0]) << "-----" << &(*itera) << endl;

} //: testContainer

void testListClass()
{
    VMan manList;
    for(int i=0; i<2; i++)
    {
        Man tempMan;
        tempMan.setName();
        cout << tempMan.scores << endl;
        manList.push_back(tempMan);
    }
    cout << manList[0].scores << endl;
    cout << manList[1].scores << endl;
}

void testVectorFree()
{
    for(int i=0; i<100000; i++)
    {
        vecb2 tempArr2;
        vecb tempArr(100);
        for(int j=0; j<100; j++)
        {
            tempArr[j] = 0.0;
        }
        tempArr2.push_back(tempArr);
    }
    double temp;
    cin >> temp;



} //: testVectorFree

void testGetTimeOfDay()
{
    struct timeval start, finish;
    gettimeofday(&start, NULL);
    double totaltime = 0.0;
    int m, n, l;
    m = 100;
    n = 100;
    l = 100;
    while(m--)
    {
        while(n--)
        {
            //while(l--);
        }
    }

    gettimeofday(&finish, NULL);
    totaltime = finish.tv_sec - start.tv_sec
        + (finish.tv_usec-start.tv_usec) / 1000000.0;
    cout << "totaltime=" << totaltime << "  sec" << endl;
}
///:~
